#!/usr/bin/env bash
#
# Copyright (c) 2024 Sergey Kvachonok
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

. "$(dirname "$0")"/msvcenv.sh

# By default, the platform-specific settings for the current project override
# the PATH, INCLUDE, LIB, LIBPATH, CONFIGURATION, and PLATFORM environment
# variables. Set the 'UseEnv' property to true to guarantee that the environment
# variables don't get overridden.
#
# https://learn.microsoft.com/en-us/cpp/build/reference/msbuild-visual-cpp-overview?view=msvc-170#useenv-property
#export UseEnv=true

# Do not try to find the VC and Windows SDK install paths in the Registry.
export DisableRegistryUse=true

# Set these MSBuild properties using environment variables so that MSVC project
# builds work the same with both `UseEnv=true` and `UseEnv=false`.
#
# These properties are used in various .props files found in `MSBuild/Microsoft/VC/v170`.
export VCToolsVersion="$MSVCVER"
export VCInstallDir_170="${MSVCBASE}\\"
export VCToolsInstallDir_170="${MSVCDIR}\\"

export MicrosoftKitRoot="${BASE}\\"
export SDKReferenceDirectoryRoot="$MicrosoftKitRoot"
export SDKExtensionDirectoryRoot="$MicrosoftKitRoot"

# These environment variables are used by the native API
# `[Microsoft.Build.Utilities.ToolLocationHelper]::GetLatestSDKTargetPlatformVersion()`.
# See https://learn.microsoft.com/en-us/dotnet/api/microsoft.build.utilities.toollocationhelper.getlatestsdktargetplatformversion?view=msbuild-17-netcore
export MSBUILDSDKREFERENCEDIRECTORY="$MicrosoftKitRoot"
export MSBUILDMULTIPLATFORMSDKREFERENCEDIRECTORY="$MicrosoftKitRoot"

# Set these manually because the above kit search does not always work.
export WindowsSdkDir_10="${SDKBASE}\\"
export UniversalCRTSdkDir_10="$WindowsSdkDir_10"
export WindowsSdkDir="$WindowsSdkDir_10"
export UniversalCRTSdkDir="$UniversalCRTSdkDir_10"
export WindowsTargetPlatformVersion="$SDKVER"
export UCRTContentRoot="$UniversalCRTSdkDir_10"
# These should point to the NETFX SDK, which may or may not be located
# in the common Windows SDK directory.
export NETFXKitsDir="$WindowsSdkDir"
export NETFXSDKDir="$WindowsSdkDir"

# WDK-specific properties:
export WDKKitVersion="10"
# No spectre-mitigated libraries in this SDK.
export Driver_SpectreMitigation=false
# Driver signing does not work with WINE crypto libraries.
export SignMode=off
export Inf2CatNoCatalog=true
# API Validator crashes in WINE.
export ApiValidator_Enable=False

# Set MSBuild-specific Platform property via an environment variable.
# 'Platform' property names do not match ARCH names exactly.
#
# https://learn.microsoft.com/en-us/cpp/build/reference/msbuild-visual-cpp-overview?view=msvc-170#support-file-directories
# https://learn.microsoft.com/en-us/cpp/build/reference/vcxproj-file-structure?view=msvc-170#projectconfiguration-elements
case ${ARCH} in
x86)
    export Platform=Win32
    ;;
arm)
    export Platform=ARM
    ;;
arm64)
    export Platform=ARM64
    ;;
*)
    export Platform=${ARCH}
    ;;
esac

if [[ "${MSBUILDBINDIR}" == *amd64 ]]; then
    # Prefer x86_64 build tools for any target arch.
    export PreferredToolArchitecture=x64
fi

export WINE_MSVC_RAW_STDOUT=1
"$(dirname "$0")"/wine-msvc.sh "${MSBUILDBINDIR}"/MSBuild.exe "$@"
